package com.yanqu.road.server.manger.activity.recycle;

import com.yanqu.road.dao.impl.activity.soulbone.SoulBoneUserDataDaoImpl;
import com.yanqu.road.entity.activity.ActivityItemRecycleData;
import com.yanqu.road.entity.activity.soulbone.SoulBoneConfig;
import com.yanqu.road.entity.activity.soulbone.SoulBoneUserData;
import com.yanqu.road.entity.enums.eActivityType;
import com.yanqu.road.entity.enums.eMailType;
import com.yanqu.road.entity.language.LanguageType;
import com.yanqu.road.entity.player.UserInfo;
import com.yanqu.road.server.logic.mail.MailManager;
import com.yanqu.road.server.manger.MultipleLanguageMgr;
import com.yanqu.road.server.manger.ServerLanguageMgr;
import com.yanqu.road.server.manger.activity.SoulBoneActivityMgr;
import com.yanqu.road.server.manger.player.UserMgr;
import com.yanqu.road.utils.property.Property;
import com.yanqu.road.utils.property.PropertyHelper;
import com.yanqu.road.utils.string.StringUtils;

import java.math.BigInteger;
import java.text.MessageFormat;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

public class SoulBoneRecycleStrategy extends AbstractItemRecycleStrategy {

    @Override
    public void markNeedRecycleItem() {
        SoulBoneConfig config = SoulBoneActivityMgr.getConfig();
        if(config != null){
            Property baseReward = PropertyHelper.parseStringToProperty(config.getGetReward());
            int boxId = 0;
            for (Map.Entry<Integer, BigInteger> entry : baseReward.getGoods().entrySet()) {
                boxId = entry.getKey();
                break;
            }
            long recycleTime = config.getActivityInfo().getEndShowTime() * 1000L + 600 * 1000;
            int activityId = config.getActivityInfo().getActivityId();
            List<String> pList = new ArrayList<>();
            pList.add(activityId + "");
            pList.add(config.getActivityInfo().getActivityName());
            ActivityItemRecycleMgr.addRecycleItemData(boxId, activityType, 5, recycleTime, pList);
        }

    }

    @Override
    protected void initActivityType() {
        this.activityType = eActivityType.SoulBoneLottery.getValue();
    }

    @Override
    protected void doRecycle(long userId, ActivityItemRecycleData data) {
        if(data.getRecycleType() == 5){
            int activityId = Integer.parseInt(data.getParamList().get(0));
            SoulBoneUserData soulBoneUserData = new SoulBoneUserDataDaoImpl().getSoulBoneUserData(activityId, userId);
            if(soulBoneUserData != null){
                if(!StringUtils.isNullOrEmpty(soulBoneUserData.getJuBao())){
                    String reward = soulBoneUserData.getJuBao();
                    soulBoneUserData.setJuBao("");

                    boolean result = new SoulBoneUserDataDaoImpl().update(soulBoneUserData);

                    if(!result){
                        log.info("SoulBoneRecycleStrategy activityId{}, update user {} fail", activityId, userId);
                        return;
                    }

                    //邮件
                    UserInfo userInfo = UserMgr.getUserInfo(userId);
                    String language = LanguageType.LANGUAGE_DEFAULT;
                    if (null != userInfo) {
                        language = userInfo.getLanguage();
                    }
                    String activityName = ServerLanguageMgr.getContent(data.getParamList().get(1), language);
                    String mailTitle = MultipleLanguageMgr.getContent(MailManager.TREASURE_ACTIVITY_PROGRESS_REWORD_TITLE, language);
                    String mailContent = StringUtils.format(MultipleLanguageMgr.getContent(MailManager.TREASURE_ACTIVITY_PROGRESS_REWORD_CONTENT, language),
                            ServerLanguageMgr.getContent(activityName, language));
                    MailManager.sendMail(userId, eMailType.SYSTEM.getValue(), reward, mailContent, mailTitle);
                }
            }
        }
    }

    @Override
    public boolean isActivityRunning() {
        return false;
    }
}
